package com.cebbank.group.demoweb.controller;


import com.cebbank.group.demoweb.commponent.LazyInitComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@RequestMapping({"/hello","/cry"})
@RestController
public class HelloController {

    @Autowired
    @Lazy
    LazyInitComponent lazyInitComponent;

    @RequestMapping({"/a","/b"})
    public String say(){
        lazyInitComponent.say();
        return "hello world";
    }

    /**
     * 测试cpu占用过高
     */
    @RequestMapping("/test")
    public String test(){
        //启动两个线程
        for (int i = 0; i < 2; i++) {
            new Thread(HelloController::method2).start();
        }
        return "运行成功";
    }
    static ConcurrentHashMap map = new ConcurrentHashMap();
    private static void method2() {
        while (true)
        {
            int i = 10000;
            while (i>0){
                map.put(Thread.currentThread().getName()+i,i);
                i--;
            }
        }
    }


    @Value("#{'${params}'.split(',')}")
    private List<String> params;

    @RequestMapping(value = "/params")
    public List<String> printListParamFromConfig(){
        String[] arr = params.toArray(new String[params.size()]);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        System.out.println(params);
        return params;
    }
}
